package com.jiuyin.dianjing.ui.activity.community;


import android.content.Intent;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.api.base.DefaultApiResult;
import com.jiuyin.dianjing.api.data.TokenJson;
import com.jiuyin.dianjing.api.enums.ApiEnum;
import com.jiuyin.dianjing.api.interfaces.ApiResultCallback;
import com.jiuyin.dianjing.api.server.ServerApi;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.event.RefreshEvent;
import com.jiuyin.dianjing.model.UniversityClub;
import com.jiuyin.dianjing.ui.activity.base.BaseActivity;
import com.jiuyin.dianjing.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import butterknife.BindView;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_CONTACT;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_GRADE;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_MESSAGE;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_PAGE_NUM;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TOKEN;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_UNIVERSITYCLUB_ID;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_WORD;

public class JoinEsportsClubActivity extends BaseActivity {

    @BindView(R.id.tv_name_right)
    TextView tvClubName;
    @BindView(R.id.tv_nickname)
    TextView tvNickName;
    @BindView(R.id.tv_username)
    TextView tvUserName;
    @BindView(R.id.et_class)
    EditText etClass;
    @BindView(R.id.et_contract)
    EditText etContract;
    @BindView(R.id.et_remark)
    EditText etRemark;
    @BindView(R.id.bt_submit)
    Button btSubmit;

    private UniversityClub club;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_join_esports_club;
    }

    @Override
    protected void initData() {
        tvUserName.setText(HelperApplication.getNickName());
        Intent intent = getIntent();
        if (intent != null) {
            club = (UniversityClub) intent.getSerializableExtra("clubModel");
            tvClubName.setText(club.name);
            tvNickName.setText(club.nickname);
            btSubmit.setOnClickListener(v -> {
                submit();
            });
        }
    }

    @Override
    protected void initView() {
        setTitle("加入电竞社");
    }

    private void submit() {
        if (TextUtils.isEmpty(etClass.getText())) {
            ToastUtil.showShort("请填写所在班级");
            return;
        }
        if (TextUtils.isEmpty(etContract.getText())) {
            ToastUtil.showShort("请填写联系方式");
            return;
        }
        HashMap<String, Object> data = new HashMap<>();
        data.put(KEY_TOKEN, HelperApplication.getToken());
        data.put(KEY_GRADE, etClass.getText().toString());
        data.put(KEY_CONTACT, etContract.getText().toString());
        data.put(KEY_UNIVERSITYCLUB_ID, club.universityclub_id);
        data.put(KEY_MESSAGE, etRemark.getText().toString());
        JSONObject jsonObject = new JSONObject(data);
        ApiResultCallback apiRequestCallback = new DefaultApiResult() {
            @Override
            public void onSuccess(JsonObject data) {
                ToastUtil.showShort("申请已发送");
                EventBus.getDefault().post(new RefreshEvent(CollegeMainActivity.class.getSimpleName()));
                finish();
            }
        };
        ServerApi.post(ApiEnum.APPLY_FOR_JOIN_UNIVERSITY_CLUB, jsonObject, null, progress, null, apiRequestCallback);
    }
}
